package com.abocode.aichat.server.modules.protocol;


import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.modules.AbstractModule;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;
import com.abocode.aichat.server.modules.AbstractModule;
import io.netty.channel.Channel;

import java.util.List;
import java.util.Map;

/**
 * Created by franky-14.
 */
public class ChatModule extends AbstractModule {

    @Override
    public String namespace() {
        return "chat";
    }

    @Override
    public String version() {
        return "1.0";
    }

//    private ChatModuleHandler chatModuleHandler = new ChatModuleHandler();
//
//    class ChatModuleHandler implements ModuleHandler {
//        @Override
//        public Replies execute(ChannelWrapper channelWrapper, Stanza stanza) {
//            if (getServerRuntimeContext().getMessageGatewayPipeline() == null) {
//                throw new NullPointerException("offline pipeline is null");
//            }
//            Map body = stanza.body();
//            if ("chat".equals(stanza.module())) {
//                return chat(channelWrapper,stanza, body);
//            } else if ("offline".equals(stanza.module())) {
//                return offline_message(channelWrapper, body);
//            }else if("confirm".equals(stanza.module())) {
//
//            } else {
//                throw new RuntimeException("no found " + stanza.module());
//            }
//        }
//
//        private Replies chat(ChannelWrapper channelWrapper,Stanza stanza, Map body) {
//            String toJid = String.valueOf(body.get("to"));
//            Entity toEntity = EntityImpl.parse(toJid);
////            if (getServerRuntimeContext().getMessageStoragePipeline() != null) {
////                getServerRuntimeContext().getMessageStoragePipeline().message(body);
////            }
//            Replies replies = new Replies();
//            Channel fromChannel = channelWrapper.getChannel();
//             //todo   online
//            List<Channel> toChannels= getServerRuntimeContext().getProtocolChannelRegistry().get(toEntity);
//            if (!getServerRuntimeContext().getProtocolChannelRegistry().isOnline(toEntity)) {
//                replies.addReply(fromChannel, StanzaWrapper.wrapperOffline(stanza));
//                stanza.setModule("offline");
//            } else {
//                replies.addReply(fromChannel, StanzaWrapper.wrapperOnline(stanza));
//                replies.addReply(toChannels, StanzaWrapper.wrapper(stanza, body));
//            }
//            getServerRuntimeContext().getMessageGatewayPipeline().push(toEntity, stanza);
//            return replies;
//        }
//
//        private Replies offline_message(ChannelWrapper channelWrapper,Map body){
//            //todo offline
//            String jid = String.valueOf(body.get("jid"));
//            Entity jidEntity = EntityImpl.parse(jid);
//            return getServerRuntimeContext().getMessageGatewayPipeline().messages(channelWrapper.getChannel(),jidEntity);
//        }
//
//
//    }
//
//    @Override
//    public ModuleHandler moduleHandler() {
//        return chatModuleHandler;
//    }
}
